package com.student.dms;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.utils.ToastUtils;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.db.PushDao;
import com.student.db.PushEntity;
import com.student.dms.databinding.ActivityAnnouncementPushBinding;
import com.student.dms.databinding.ActivityEditPushBinding;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Observable;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ActivityEditPush extends BaseActivity {

    private ActivityEditPushBinding activityEditPushBinding;
    private String picPath;
    private PushEntity pushData;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityEditPushBinding = ActivityEditPushBinding.inflate(getLayoutInflater());
        setContentView(activityEditPushBinding.getRoot());
        Intent intent = getIntent();
        pushData = intent.getParcelableExtra("pushData");
        if(pushData!=null){
            //显示前面跳转的内容
            activityEditPushBinding.editTitle.setText(pushData.getTitle());
            activityEditPushBinding.editContent.setText(pushData.getContent());
            picPath = pushData.getImagePath();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap = BitmapFactory.decodeFile(picPath);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            activityEditPushBinding.preview.setImageBitmap(bitmap);
                        }
                    });
                }
            }).start();

        }

        activityEditPushBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(TitleBar titleBar) {
                finish();
            }
        });
        activityEditPushBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editTitle = activityEditPushBinding.editTitle.getText().toString().trim();
                String editContent = activityEditPushBinding.editContent.getText().toString().trim();
                if (checkInputIsNull(editContent) || checkInputIsNull(editTitle)) {
                    ToastUtils.showToast(ActivityEditPush.this, "标题和内容不能为空！");
                    return;
                }
                if (checkInputIsNull(picPath) ) {
                    ToastUtils.showToast(ActivityEditPush.this, "请选择公告图片！");
                    return;
                }

                if(null==pushData){
                    pushData=new PushEntity();
                }
                pushData.setTitle(editTitle);
                pushData.setContent(editContent);
                pushData.setImagePath(picPath);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String time = simpleDateFormat.format(new Date());
                pushData.setPushTime(time);
                AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
                PushDao pushDao = roomDatabase.providePushDao();
                disposable.add(pushDao.insert(pushData).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(()->{
                    ToastUtils.showToast(ActivityEditPush.this, "发布公告成功！");
                    ActivityEditPush.this.finish();
                }));
            }
        });
        activityEditPushBinding.addPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PictureSelector.create(ActivityEditPush.this)
                        .openGallery(SelectMimeType.ofImage())
                        .setImageEngine(new GlideEngine())
                        .setMaxSelectNum(1)
                        .forResult(new OnResultCallbackListener<LocalMedia>() {
                            @Override
                            public void onResult(ArrayList<LocalMedia> result) {
                                LocalMedia localMedia = result.get(0);
                                picPath = localMedia.getRealPath();
                                Bitmap bitmap = BitmapFactory.decodeFile(localMedia.getRealPath());
                                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
                                activityEditPushBinding.preview.setImageBitmap(bitmap);
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
            }
        });
    }

    private boolean checkInputIsNull(String editText) {
        return TextUtils.isEmpty(editText);
    }
}
